package com.tom.convertor;

import org.springframework.core.convert.converter.Converter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class DateConvertor implements Converter<String,Date> {

    private List<String> patterns;

    public void setPatterns(List<String> patterns) {
        this.patterns = patterns;
    }

    public Date convert(String sourse) {
        Date result = null;
        for (String pattern : patterns) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            try {
                result = simpleDateFormat.parse(sourse);
                System.out.println("日期转换结果是:" + result);
                return result;
            } catch (Exception e) {
                System.out.println("日期转换异常:" + e.getMessage());
            }
        }
        return result;
    }
}
